/**
 * 2009 圣诞活动
 */
$(function() {
    // inject http://res.61.com/css/dwg/special.css
    try {
        //var elm_style = $('<link type="text/css" rel="stylesheet" href="http://res.61.com/css/dwg/special.css" />');
        var elm_style = $('<link />').attr('type', 'text/css').attr('rel', 'stylesheet').attr('href', 'http://res.61.com/css/dwg/special.css');
        $(document.getElementsByTagName('head')).eq(0).append(elm_style);
    } catch(err) {}
    // preload images
    try {
        var prefix = 'http://res.61.com/images/dwg/special/200912/'
        var images = [
            'box_christmas_body.gif',
            'box_christmas_bottom.gif',
            'box_christmas_top.gif',
            'christmas.gif',
            'christmas_bj.gif',
            'christmas_mz.gif'
        ];
        $(images).each(function(index, item) {
            $(new Image()).attr('src', prefix + item);
        });
    } catch(err) {}

    // popups
    var OFFSET_TOP = 100;           // 弹出框到顶部距离，单位：px
    var OFFSET_LEFT = -225;
    var mask = $('<div></div>')
                            .css('position', 'absolute')
                            .css('top', 0)
                            .css('left', 0)
                            .css('z-index', 11)
                            .css('width', '100%')
                            .css('height', '100%')
                            .css('background-color', '#333')
                            .css('opacity', 0.8)
                            .hide()
                            .appendTo(document.body);
    var popup_container = $('<div></div>')
                            .css('position', 'absolute')
                            .css('z-index', 12)
                            .css('left', '50%');
    var popup_ph = $('<div></div>').hide().appendTo(document.body);

    var hideSWFs = function() {
        window.hide_image_switcher = true;
        $('.left_column .tv .show *').hide();
    };
    var showSWFS = function() {
        window.hide_image_switcher = false;
        $('.left_column .tv .show *').show();
    };

    var show_mask = function() {
        mask.css('width', $(document).width())
            .css('height',$(document).height())
            .show();
        hideSWFs();
    };
    var hide_mask = function() {
        mask.hide();
        showSWFS();
    };

    var hide_popup = function() {
        popup_ph.empty().hide();
        hide_mask();
    };
    var show_popup = function(element) {
        show_mask();
        popup_ph.show();
        popup_container.append($(element).show())
                          .appendTo(document.body)
                          .css('top', OFFSET_TOP + $(document).scrollTop())
                          .css('margin-left', OFFSET_LEFT)
                          .fadeIn('fast');
    };

    $(document).keydown(function(e) {
        if (e.keyCode == 27 )
        {
            hide_popup();
        }
    });

    var rebind_events = function(container) {
        if ( !container ) container = document.body;
        container = $(container);

        $('a#close', container).bind('click', function(e) {
            e.preventDefault();
            hide_popup();
        });

        $('a#clain', container).bind('click', function(e) {
            e.preventDefault();
            var id = $('input[name=id]', $(this).closest('.box_christmas')).eq(0).val();
            clain(id);
            return false;
        });

        $('a#login', container).bind('click', function(e) {
            e.preventDefault();
            var url = '/user/loginByAjax';
            var id = $('input[name=id]', $(this).closest('.box_christmas')).eq(0).val();
            var uid = $('input[name=uid]', $(this).closest('.box_christmas')).eq(0).val();
            var pwd = $('input[name=pwd]', $(this).closest('.box_christmas')).eq(0).val();
            var payload = { uid: uid, pwd: pwd, is_ajax_login: 1 };
            $.post(url, payload, function(data) {
                var res = eval('(' + data + ')');
                // 登录失败，显示错误提示
                if (!res || res['status_code']) {
                    var msg = (res && res['msg']) ? res['msg'] : '系统错误';
                    var callback = function() {
                        hideSWFs();
                    };
                    $.Alert.show(msg, '错误', 'ERROR', callback, callback, callback);
                }
                else
                {
                    clain(id);
                    try {
                        $.init_panel();
                    } catch(err) {}
                }
            });
            return false;
        });
    };

    var clain = function(id) {
        if ( !id ) return;
        var url = '/seasonal/xmas2009';
        var payload = { id: id };

        $.post(url, payload, function(data) {
            if ( !data ) return;
            popup_ph.html(data);
            show_popup(popup_ph);

            rebind_events(popup_ph);
        });
    };

    var check = function(id) {
        if ( !id ) return;
        var url = '/seasonal/xmas2009';
        var payload = { id: id };

        $.get(url, payload, function(data) {
            if ( !data ) return;
            popup_ph.html(data);
            show_popup(popup_ph);

            rebind_events(popup_ph);
        });
    };

    $('#present_1').click(function(e) {
        e.preventDefault();
        check(1);
    });

    $('#present_2').click(function(e) {
        e.preventDefault();
        check(2);
    });

    // global function for swf
    window.check_present = function(id) {
        if ( !id ) id = 2;
        check(id);
        return false;
    };
});
